
package com.sky.security.service.impl;


import com.sky.entity.Employee;
import com.sky.exception.PasswordErrorException;
import com.sky.mapper.EmployeeMapper;
import com.sky.security.domain.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Service
public class UserDetailsServiceImpl implements UserDetailsService {

    @Autowired
    private EmployeeMapper employeeMapper;

   /* @Autowired
    private MenuMapper menuMapper;*/

    @Override
    public LoginUser loadUserByUsername(String username) {

        //根据用户名查询用户信息

        Employee employee = employeeMapper.selectOne1(username);
        //如果查询不到数据就通过抛出异常来给出提示
        if (Objects.isNull(employee)) {
            //密码错误
            throw new PasswordErrorException("用户名或密码错误");
        }
        //TODO 根据用户查询权限信息 添加到LoginUser中

        //封装成UserDetails对象返回
        //List<String> list = menuMapper.selectPermsByUserId(user.getId());
        List<String> list = new ArrayList<>();
        list.add("sys:list:select");
        //把数据封装成UserDetails返回
        return new LoginUser(employee, list);
    }
}

